<?php
/*
	@author: Peter.Nguyen
	@date: 5/6/2012
*/
class AutoPick implements DomHTML
{
	private $domDocument;
	private $dom;
	private $url;
	public $domain = "www.pick.vn";

	public function __construct($url=""){
		
		$this->domDocument  = new DOMDocument();
		$this->domDocument->formatOutput = true;
		$this->dom = new DOMDocument();
		$this->dom->preserveWhiteSpace = false;
				
	}
	public function setFile($url){
		$this->url = $url;
		@$this->dom->loadHTMLFile($url);
	}
	public function getTitle($data){
	
	}
	public function getImage($data){
	
	}
	public function getDate($data){
	
	}	
	public function getLink($data){
	
	}
	public function getSummarization($data){
	
	}
	public function utf8encode($string){
		return utf8_encode($string);
	}
	public function getNodeHtml( $node ) {
		$innerHTML = "";
		$children = $node->childNodes;
		foreach ($children as $child) {
			$childData = trim($child->ownerDocument->saveXML($child));
			if($childData!="&#13;"&&$childData!=""){
				$innerHTML .= $childData;
			}
		}
		return $innerHTML;
	}
	public function getTags($tagName, $attrName, $attrValue ){
		
		$html = '';
		$domxpath = new DOMXPath($this->dom);
		if($attrName&&$attrValue){
			$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
		}else{
			$filtered = $domxpath->query("//$tagName");	
		}
		$i = 0;
		$arrData = array();
		while( $myItem = $filtered->item($i++) ){
		   $node = $this->domDocument->importNode( $myItem, true ); 
		   $arrData[] = $this->getNodeHtml($node);
		}
		return $arrData;
		
	}
	
	public function getTagsFromString($string, $tagName, $attrName, $attrValue ){
		
		$html = '';
		$dom = new DOMDocument();
		$string = mb_convert_encoding($string, 'HTML-ENTITIES', "UTF-8"); 
		$dom->loadHTML($string);
		$domxpath = new DOMXPath($dom);
		if($attrName&&$attrValue){
			$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
		}else{
			$filtered = $domxpath->query("//$tagName");	
		}
		$i = 0;
		$arrData = array();
		while( $myItem = $filtered->item($i++) ){
		   $node = $this->domDocument->importNode( $myItem, true ); 
		   $arrData[] = $this->getNodeHtml($node);
		}
		return $arrData;
		
	}	
	
	public function getInfoTagsFromString($attr, $string, $nameTag, $nameCondition, $valCondition){
		
		$arrData = array();
		$valTag 	= $this->getTagsFromString($string, $nameTag, $nameCondition, $valCondition);
		$valAttr	= $this->getAttrFromString($attr, $string, $nameTag, $nameCondition, $valCondition);
		
		foreach( $valTag as $key => $val ){
			$arrData[] = array($attr => $valAttr[$key], "value"=>$val);	
		}
		return $arrData;
		
	}
	
	public function getAttrFromString($nameAttr, $string, $tagName, $attrName, $attrValue ){
		
		$html = '';
		$dom = new DOMDocument();
		$string = $this->utf8encode($string);
		$dom->loadHTML($string);
		$domxpath = new DOMXPath($dom);
		if($attrName&&$attrValue){
			$filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
		}else{
			$filtered = $domxpath->query("//$tagName");	
		}
		$i = 0;
		$arrData = array();
		while( $myItem = $filtered->item($i++) ){
		   $node = $this->domDocument->importNode( $myItem, true ); 
		   $arrData[] = ($nameAttr=="href" || $nameAttr=="src")?$this->domain.$node->getAttribute($nameAttr):$node->getAttribute($nameAttr);
		}
		return $arrData;
		
	}	
}
?>
